// fork_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8888

int main(int argc, char const *argv[]) {
    struct sockaddr_in serv_addr;
    int sock = 0;
    char buffer[1024] = {0};
    const char *hello = "Hello from client";

    if (argc != 2) {
        printf("Usage: %s <IP Address>\n", argv[0]);
        return -1;
    }

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket creation error");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0) {
        perror("invalid address/ address not supported");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("connection failed");
        return -1;
    }

    send(sock, hello, strlen(hello), 0);
    printf("Hello message sent to server\n");

    int valread = read(sock, buffer, 1024);
    printf("Server response: %s\n", buffer);

    close(sock);
    return 0;
}
